package onebyone;

import org.apache.ibatis.session.SqlSession;
import util.MybatisUtil;

public class StuCardDao {
	/**
	 * 通过学生ID查找学生信息
	 */
	public Stu findById(int id) throws Exception{
		SqlSession sqlSession=null;
		try {
			 sqlSession=MybatisUtil.getSqlSession();
			return sqlSession.selectOne("stuNamespace.findById", id);
		} catch (Exception e) {
			e.printStackTrace();		
			throw e;
		}finally {
			MybatisUtil.closeSqlSession();
		}
		
	}
	
	/**
	 * 通过学生姓名查找学生信息
	 */
	public Stu findByName(String name) throws Exception{
		SqlSession sqlSession=null;
		try {
			 sqlSession=MybatisUtil.getSqlSession();
			return sqlSession.selectOne("stuNamespace.findByName",name);
		} catch (Exception e) {
			e.printStackTrace();		
			throw e;
		}finally {
			MybatisUtil.closeSqlSession();
		}
		
	}
	
	
	/**
	 * 测试
	 */
	
	public static void main(String[] args) throws Exception{
		StuCardDao dao=new StuCardDao();
		Stu s=dao.findById(1);
		System.out.println(s.getId()+":"+s.getName()+":"+s.getCard().getNum());
		System.out.println("------------------------------");
	    s=dao.findByName("哈哈");
	    System.out.println(s.getId()+":"+s.getName()+":"+s.getCard().getNum());
	}

}
